package cn.cdnsu.auth.controller;

import cn.cdnsu.auth.annotation.NsuPermission;
import cn.cdnsu.auth.domain.Role;
import cn.cdnsu.auth.dto.RoleDTO;
import cn.cdnsu.auth.query.RoleQuery;
import cn.cdnsu.auth.service.IRoleServiceI;
import cn.cdnsu.base.util.AjaxResult;
import cn.cdnsu.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@NsuPermission(name = "角色管理",descs = "角色管理api")
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleServiceI service;
    @NsuPermission(name="角色分页查询")
    @PutMapping
    public AjaxResult QueryPage(@RequestBody RoleQuery roleQuery){
        try {
            PageInfo<Role> pageInfo=service.queryPage(roleQuery);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name="批量删除角色")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name="查询全部角色")
    @GetMapping
    public AjaxResult selectAll(){
        try {
            return AjaxResult.success(service.selectAll());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name="查询指定角色")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            return AjaxResult.success(service.selectById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name="角色新增或修改")
    @PostMapping
    public AjaxResult insertOrUpdate(@RequestBody Role role){
        try {
            if (Objects.nonNull(role)){
                if (Objects.nonNull(role.getId())){
                    service.update(role);
                }else {
                    service.insert(role);
                }
                return AjaxResult.success();
            }
            return AjaxResult.error("参数错误");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @NsuPermission(name="删除指定角色")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


    //保存权限

    //使用DTO接收
    //使用VO你的返回值给页面的数据，也不是domain的情况
    @NsuPermission(name="授权的新增或修改")
    @PostMapping("/permission")
    public AjaxResult insertByRoleId(@RequestBody RoleDTO roleDTO) {
        try {
            service.insertByRoleId(roleDTO);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    //根据角色ID查询对应的权限
    @NsuPermission(name="根据角色ID查询对应权限")
    @GetMapping("/permission/{id}")
    public AjaxResult selectByRoleId(@PathVariable("id") Long id) {
        try {
            List<String> sns=service.selectByRoleId(id);
            return AjaxResult.success(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }


}
